import 'package:dio/dio.dart';
import 'package:mall/core/models/business_condition.dart';
import 'package:mall/core/models/business_level.dart';
import 'package:mall/core/services/api.dart';
import 'package:mall/core/providers/view_model.dart';
import 'package:mall/core/services/new_api.dart';

class BusinessConditionModel extends ViewModel {
  final NewApi api;
  final BusinessLevel businessLevel;

  String _errorMessage;
  BusinessCondition _businessCondition;

  BusinessConditionModel(this.api, this.businessLevel) : super() {
    _initDate();
  }

  String get errorMessage => _errorMessage;

  BusinessCondition get businessCondition => _businessCondition;

  Future<ViewState> _initDate() async {
    viewState = ViewState.busy;
    try {
      _businessCondition = await api.businessCondition(
        businessLevel.businessType,
        businessLevel.businessLevel,
      );
      viewState = ViewState.success;
    } catch (e) {
      _errorMessage = e is DioError ? e.message : e.toString();
      viewState = ViewState.error;
    }
    return viewState;
  }
}
